import com.adobe.cairngorm.control.CairngormEventDispatcher;

import mx.events.ValidationResultEvent;

import net.optix.ip.events.UserLoginEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.UserVO;

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

[Bindable]
private var welcome:String	=	
	'Ingenuity Nexus helps growing businesses expand, manage, and profit from their intellectual property. \n\n'+
	' You can record a disclosure of each of your ideas and calculate their potential value.  Recording your ideas encourages more and better ideas.  A designated manager for your business can track the ideas and inventions, and if desired, assign an invention to a law firm or request bids from multiple law firms for preparation and filing of a patent application.  Ingenuity Nexus does not provide legal services, but can connect your business with cost-effective legal representation.';

private function initApp():void
{
	model.result	=	"";
	model.bSubmit	=	true;
}
public function login():void
{
	var result:Boolean	=	true;

	var resultValidate:ValidationResultEvent;
	model.result			=	"";
				
	resultValidate	=	emailValidator.validate(email.text);
 	if(resultValidate.type == ValidationResultEvent.INVALID)
	{
     	result	=	false;
     	email.errorString	=	resultValidate.message;
	}
	else
	{
		email.errorString	=	"";
	}
            	
	stringValidator.minLength	=	6;
  	resultValidate	=	stringValidator.validate(password.text);
  	if(resultValidate.type == ValidationResultEvent.INVALID)
   	{
     	result	=	false;
     	password.errorString	=	resultValidate.message;
	}
	else
	{
		password.errorString	=	"";
	}
            	
	model.bSubmit	=	false;
	model.result	=	"";
            	
  	if(result == true)
  	{
     	var vo:UserVO	=	new	UserVO();
     	vo.email	=	email.text;
       	vo.password	=	password.text;
            		
     	var event:UserLoginEvent	=	new UserLoginEvent(vo);
	   	CairngormEventDispatcher.getInstance().dispatchEvent( event );
	   	
	   	model.result	=	"logging in ...";
   	}
	else
 	{
      	model.bSubmit	=	true;
  	}
}

public function loginForPractitioner():void
{
	var result:Boolean	=	true;

	var resultValidate:ValidationResultEvent;
	model.result			=	"";
				
	resultValidate	=	emailValidator.validate(emailPractitioner.text);
 	if(resultValidate.type == ValidationResultEvent.INVALID)
	{
     	result	=	false;
     	email.errorString	=	resultValidate.message;
	}
	else
	{
		email.errorString	=	"";
	}
            	
	stringValidator.minLength	=	6;
  	resultValidate	=	stringValidator.validate(passwordPractitioner.text);
  	if(resultValidate.type == ValidationResultEvent.INVALID)
   	{
     	result	=	false;
     	password.errorString	=	resultValidate.message;
	}
	else
	{
		password.errorString	=	"";
	}
            	
	model.bSubmit	=	false;
	model.result	=	"";
            	
  	if(result == true)
  	{
     	var vo:UserVO	=	new	UserVO();
     	vo.email	=	emailPractitioner.text;
       	vo.password	=	passwordPractitioner.text;
            		
     	var event:UserLoginEvent	=	new UserLoginEvent(vo);
	   	CairngormEventDispatcher.getInstance().dispatchEvent( event );
	   	
	   	model.result	=	"logging in ...";
   	}
	else
 	{
      	model.bSubmit	=	true;
  	}
}

private function showPractitionerLogin():void
{
	loginViews.selectedIndex	=	1;
}

private function showClientLogin():void
{
	loginViews.selectedIndex	=	0;
}